package com.a3dweijing.wetouch2.Activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.AlerDialogHelp;
import com.a3dweijing.wetouch2.Util.GlideUtil;
import com.a3dweijing.wetouch2.Util.ProgressDialogHelp;
import com.a3dweijing.wetouch2.network.RetrofitUtil;
import com.a3dweijing.wetouch2.Util.Util;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.OrderListBean;
import com.a3dweijing.wetouch2.bean.ResultMsg;
import com.a3dweijing.wetouch2.datasave.Singleton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 评价页面
 */

public class EvaluateGoodsActivity extends BaseActivity
{
    
    @BindView(R.id.back)
    ImageView back;
    
    @BindView(R.id.top_text)
    TextView topText;
    
    @BindView(R.id.top_all)
    RelativeLayout topAll;
    
    @BindView(R.id.imageview)
    ImageView imageview;
    
    @BindView(R.id.edittext)
    EditText edittext;
    
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    
    @BindView(R.id.top_right)
    TextView topRight;
    
    @BindView(R.id.rbar)
    RatingBar rbar;
    
    private OrderListBean item;
    
    private AlertDialog dialog;
    
    private ProgressDialog progressDialog;
    
    public static int EVALUTEGOODSACTIVITYCODE = 14;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate_goods);
        ButterKnife.bind(this);
        item = (OrderListBean)getIntent().getSerializableExtra("item");
        if (item == null)
            return;
        initView();
    }
    
    private void initView()
    {
        topText.setText("商品评价");
        topRight.setText("提交");
        GlideUtil.getInstance().showImage(this, item.getGoods_imgurl(), imageview);
        dialog = AlerDialogHelp.create(this, "提示", "您是否要提交吗", true, callBack);
        progressDialog = ProgressDialogHelp.create(this, "提示", "提交中...", false);
    }
    
    public static void startActivity(Activity context, OrderListBean orderListBean)
    {
        if (Util.isFastClick())
        {
            Intent intent = new Intent(context, EvaluateGoodsActivity.class);
            intent.putExtra("item", orderListBean);
            context.startActivityForResult(intent, EVALUTEGOODSACTIVITYCODE);
        }
    }
    
    @OnClick({R.id.back, R.id.top_right})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.back:
                setResult(RESULT_CANCELED);
                finish();
                break;
            case R.id.top_right:
                dialog.show();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        finish();
    }

    CallBack callBack = new CallBack()
    {
        @Override
        public void doNext(int what, Object obj)
        {
            sbmint();
        }
    };
    
    private void sbmint()
    {
        progressDialog.show();
        Observable<ResultMsg> observable =
            RetrofitUtil.getInstance().getApiOfPhp().evaluate(edittext.getText().toString(),
                item.getGoods_id(),
                Integer.parseInt(Singleton.getInstance().getUser().getUser_id()),
                (int)rbar.getRating(),
                item.getOrder_id());
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(
            new Observer<ResultMsg>()
            {
                @Override
                public void onSubscribe(Disposable d)
                {
                    disposables.add(d);
                }
                
                @Override
                public void onNext(ResultMsg resultMsg)
                {
                    if (resultMsg.isFlag())
                    {
                        EvaluateGoodsActivity.this.setResult(RESULT_OK);
                        Toast.makeText(EvaluateGoodsActivity.this, resultMsg.getMessage(), Toast.LENGTH_SHORT).show();
                        finish();
                    }
                    else
                    {
                        Toast.makeText(EvaluateGoodsActivity.this, resultMsg.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                
                @Override
                public void onError(Throwable e)
                {
                    progressDialog.dismiss();
                }
                
                @Override
                public void onComplete()
                {
                    progressDialog.dismiss();
                }
            });
    }
    
}
